package com.passioneffect.widgets {
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.GradientType;
	import flash.display.SpreadMethod;
	import flash.display.Sprite;
	import flash.events.TimerEvent;
	import flash.geom.Matrix;
	import flash.utils.Timer;
	
	import com.passioneffect.utils.DoLater;	

	/**
	 * @author Passion Effect
	 */
	public class Reflection {

		private var reflection:Sprite;
		private var target : DisplayObject;
		private var bitmapData:BitmapData;
		private var bitmap:Bitmap;
		private var bitmapCon:Sprite;
		private var gradient:Sprite;
		private var alphas:Array=[1,0];
		private var ratios:Array=[0,255];
		private var distance:Number;
		private var opened:Boolean=false;
		private var updateTime:Number = 0;
		private var timer:Timer;
		
		public function Reflection(target : DisplayObject,alpha:Number,ratio:Number,distance:Number,updateTime:Number=0) {
			this.target=target;
			this.reflection=new Sprite();
			this.reflection.mouseEnabled=false;
			this.reflection.mouseChildren=false;
			this.alphas=[alpha,0];
			this.ratios=[0,ratio];
			this.distance=distance;
			this.updateTime=updateTime;
		}

		public function open():void{
			opened=true;
			update();
			if(updateTime){
				timer=new Timer(updateTime);
				timer.addEventListener(TimerEvent.TIMER,timeUpdate);
				timer.start();
			}
		}
		
		private function timeUpdate(event : TimerEvent) : void {
			update();
		}
		
		private function draw():void{
			if(!opened)return;
			if(bitmapCon)bitmapCon.parent.removeChild(bitmapCon);
			
			var fillType : String = GradientType.LINEAR;
			var colors : Array = [0x000000,0x000000];
			var spreadMethod : String = SpreadMethod.PAD;
			var matr : Matrix = new Matrix();
			
			var ow:Number=target.width;
			var oh:Number=target.height;
			
			target.scaleX=1;
			target.scaleY=1;
			
			bitmapCon=new Sprite();
			bitmapData=new BitmapData(target.width,target.height,true,0x000000);
			
			bitmapData.draw(target);
			
			target.width=ow;
			target.height=oh;
			
			bitmap=new Bitmap(bitmapData,"never",true);
			
			bitmap.width=ow;
			bitmap.height=oh;
			bitmapCon.addChild(bitmap);
			
			gradient=new Sprite();
			matr.createGradientBox(ow,oh, (270 / 180) * Math.PI, 0, 0);
			gradient.graphics.beginGradientFill(fillType, colors, alphas, ratios, matr, spreadMethod);
			gradient.graphics.drawRect(0, 0, ow, oh);
			gradient.cacheAsBitmap = true;
			bitmapCon.addChild(gradient);
			
			bitmap.cacheAsBitmap=true;
			bitmap.mask=gradient;
			
			bitmapCon.scaleY=-1;
			bitmapCon.y=oh*2-1+distance;
			target.parent.addChild(bitmapCon);
			
			
			bitmapCon.mouseEnabled=false;
			gradient.mouseEnabled=false;
			gradient.mouseChildren=false;
			bitmapCon.mouseChildren=false;
		}
		
		public function update() : void {
			if(target.stage)DoLater.push(target.stage,draw);
		}
		
		public function close() : void {
			opened=false;
			if(bitmapCon.parent)bitmapCon.parent.removeChild(bitmapCon);
			if(timer)timer.removeEventListener(TimerEvent.TIMER,timeUpdate);
		}
	}
}
